<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>021todos初版 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前言日志，各位看官就当乐子看吧。 正经人谁写日记啊？！！    ——鹅城县长 todos项目初版基本好了，代码中保留了axios的代码，之后找找如何正确在uniapp中使用axios。 使用组件的一个难点就是很难操作其生成的dom对象。 有两个需求，没实现：  当事项完成时，uni-easyinput生成的input能加上删除线，即text-decoration:line-through; 方案是">
<meta property="og:type" content="article">
<meta property="og:title" content="021todos初版">
<meta property="og:url" content="http://example.com/2023/03/22/021todos%E5%88%9D%E7%89%88/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前言日志，各位看官就当乐子看吧。 正经人谁写日记啊？！！    ——鹅城县长 todos项目初版基本好了，代码中保留了axios的代码，之后找找如何正确在uniapp中使用axios。 使用组件的一个难点就是很难操作其生成的dom对象。 有两个需求，没实现：  当事项完成时，uni-easyinput生成的input能加上删除线，即text-decoration:line-through; 方案是">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/t0o-yang/Images/raw/master/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230320223136.jpg">
<meta property="article:published_time" content="2023-03-22T13:42:24.849Z">
<meta property="article:modified_time" content="2023-03-24T09:04:29.538Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/t0o-yang/Images/raw/master/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230320223136.jpg">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-021todos初版" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2023/03/22/021todos%E5%88%9D%E7%89%88/" class="article-date">
  <time class="dt-published" datetime="2023-03-22T13:42:24.849Z" itemprop="datePublished">2023-03-22</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E7%99%BE%E6%97%A5%E5%8D%9A%E5%AE%A2%E8%AE%A1%E5%88%92/">百日博客计划</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      021todos初版
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>日志，各位看官就当乐子看吧。</p>
<p><strong>正经人谁写日记啊？！！</strong>    ——鹅城县长</p>
<h1 id="todos"><a href="#todos" class="headerlink" title="todos"></a>todos</h1><p>项目初版基本好了，代码中保留了<code>axios</code>的代码，之后找找如何正确在<code>uniapp</code>中使用<code>axios</code>。</p>
<p>使用组件的一个难点就是很难操作其生成的dom对象。</p>
<p>有两个需求，没实现：</p>
<ol>
<li>当事项完成时，<code>uni-easyinput</code>生成的<code>input</code>能加上删除线，即<code>text-decoration:line-through;</code><br> 方案是操作dom，但是只限于h5平台，微信小程序中，无法使用DOM，然后尝试用uniapp内置的获取节点方法<code>uni.createSelectorQuery()</code>，但取不到对应的input（h5下，是通过给组件绑定id，但是微信下，根本没有id属性）； <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取dom，添加删除线</span></span><br><span class="line"><span class="keyword">const</span> input=<span class="variable language_">document</span>.<span class="title function_">querySelector</span>(<span class="string">&#x27;#todo-&#x27;</span>+todo.<span class="property">_id</span>).<span class="title function_">querySelector</span>(<span class="string">&quot;input.uni-input-input&quot;</span>)</span><br><span class="line"><span class="keyword">if</span>(input.<span class="title function_">getAttribute</span>(<span class="string">&#x27;style&#x27;</span>)==<span class="literal">null</span>) &#123;</span><br><span class="line">    input.<span class="title function_">setAttribute</span>(<span class="string">&#x27;style&#x27;</span>,<span class="string">&#x27;text-decoration:line-through;&#x27;</span>)</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    input.<span class="title function_">removeAttribute</span>(<span class="string">&#x27;style&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>数据初始化后，根据事项完成状态，添加删除线<br> 方案是操作dom, 在mounted中，获取生成的dom对象。结果发现没有获取到。</li>
<li>使用<code>axios</code>发送请求<br> 实际中也有需要将vue项目改造成uniapp项目，除了改组件，JS代码都是希望仅可能复用的，找了第三方插件，如<code>uniapp-axios-adapter</code>，没实现。说一下使用过程，大家看看哪里不对：<ol>
<li><p>安装<code>npm i axios@0.27.2</code>， <code>npm i uniapp-axios-adapter</code></p>
</li>
<li><p><code>main.js</code>中配置全局API，按照原先vue项目的使用习惯</p>
<ol>
<li>配置  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// main.js        </span></span><br><span class="line"><span class="keyword">import</span> request <span class="keyword">from</span> <span class="string">&quot;/common/http.js&quot;</span>;</span><br><span class="line"><span class="comment">// ...</span></span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">...<span class="title class_">App</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.$mount()</span><br><span class="line"><span class="comment">// axios 全局引用</span></span><br><span class="line">app.<span class="property">config</span>.<span class="property">globalProperties</span>.<span class="property">$axios</span> = request;</span><br></pre></td></tr></table></figure>
  <code>http.js</code>文件  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// /common/http.js</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&quot;uniapp-axios-adapter&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> request = axios.<span class="title function_">create</span>(&#123;</span><br><span class="line"><span class="attr">baseURL</span>: <span class="string">&quot;http://127.0.0.1:3000&quot;</span>,</span><br><span class="line"><span class="attr">timeout</span>: <span class="number">10000</span>,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">request.<span class="property">interceptors</span>.<span class="property">request</span>.<span class="title function_">use</span>(<span class="function">(<span class="params">config</span>) =&gt;</span> &#123;</span><br><span class="line"><span class="comment">//带上token</span></span><br><span class="line"><span class="comment">// config.headers[&quot;Authorization&quot;] = &quot;token&quot;;</span></span><br><span class="line"><span class="keyword">return</span> config;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">request.<span class="property">interceptors</span>.<span class="property">response</span>.<span class="title function_">use</span>(<span class="function">(<span class="params">response</span>) =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// 统一处理响应,返回Promise以便链式调用</span></span><br><span class="line"><span class="keyword">if</span> (response.<span class="property">status</span> === <span class="number">200</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; data &#125; = response;</span><br><span class="line">    <span class="keyword">if</span> (data &amp;&amp; data.<span class="property">code</span> === <span class="number">200</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">resolve</span>(data);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>(data);</span><br><span class="line">    &#125;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>(response);</span><br><span class="line">&#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> request;</span><br></pre></td></tr></table></figure></li>
<li>使用，直接调post或者按照文档方式都结果失败了 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 直接调</span></span><br><span class="line">axios.<span class="title function_">post</span>(<span class="string">&#x27;/task-modify&#x27;</span>, &#123;</span><br><span class="line">    <span class="attr">id</span>: <span class="variable language_">this</span>.<span class="property">todos</span>[index].<span class="property">_id</span>,</span><br><span class="line">    <span class="attr">title</span>: editText</span><br><span class="line">&#125;).<span class="title function_">then</span>()</span><br><span class="line"><span class="comment">// 文档案例的方式</span></span><br><span class="line"><span class="variable language_">this</span>.$axios(&#123;</span><br><span class="line">    <span class="attr">method</span>: <span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">    <span class="attr">url</span>: <span class="string">&quot;/task-del&quot;</span>,</span><br><span class="line">    <span class="attr">params</span>: &#123;</span><br><span class="line">        <span class="attr">id</span>: <span class="variable language_">this</span>.<span class="property">todos</span>[delIndex].<span class="property">_id</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;).<span class="title function_">then</span>()</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>采取了直接使用的方式</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 官方文档的使用案例</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&quot;uniapp-axios-adapter&quot;</span>;</span><br><span class="line">axios.<span class="title function_">get</span>(&#123;</span><br><span class="line">    <span class="attr">url</span>: <span class="string">&quot;https://example.com/api/getUserById&quot;</span>,</span><br><span class="line">    <span class="attr">params</span>: &#123;</span><br><span class="line">        <span class="attr">id</span>: <span class="number">1</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p> 报错是，get is not defined，这就很疑惑了！</p>
</li>
</ol>
</li>
</ol>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>三天，学习<code>uniapp</code>，使用其组件，简单实现一个todos项目</p>
<p><img src="https://gitee.com/t0o-yang/Images/raw/master/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230320223136.jpg" alt="todos真机演示"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2023/03/22/021todos%E5%88%9D%E7%89%88/" data-id="clfjqowuh000kewaa0h7h343e" data-title="021todos初版" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/03/22/022%E7%B3%BB%E7%BB%9F%E5%AD%A6%E4%B9%A0uniapp/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          022系统学习uniapp
        
      </div>
    </a>
  
  
    <a href="/2023/03/22/020uniapp%E5%89%8D%E7%AB%AF%E8%AF%B7%E6%B1%82/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">020uniapp前端请求</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%99%BE%E6%97%A5%E5%8D%9A%E5%AE%A2%E8%AE%A1%E5%88%92/">百日博客计划</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/ES6/" rel="tag">ES6</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/" rel="tag">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/uniapp/" rel="tag">uniapp</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag">前端</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/ES6/" style="font-size: 10px;">ES6</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/uniapp/" style="font-size: 10px;">uniapp</a> <a href="/tags/%E5%89%8D%E7%AB%AF/" style="font-size: 10px;">前端</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/03/">March 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/03/24/025uniapp-done/">025uniapp-done</a>
          </li>
        
          <li>
            <a href="/2023/03/23/024uniapp%E7%AC%AC%E4%B8%89%E5%A4%A9/">024uniapp第三天</a>
          </li>
        
          <li>
            <a href="/2023/03/22/023gitee-hexo%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/">023gitee+hexo搭建个人博客</a>
          </li>
        
          <li>
            <a href="/2023/03/22/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8MDN%E6%96%87%E6%A1%A3/">(no title)</a>
          </li>
        
          <li>
            <a href="/2023/03/22/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>